const FFI = require('ffi-napi');
const ref = require('ref-napi');
const path = require('path');
const DLL_PATH = path.join('D:\\kl-storage', 'dll');

export const wafer = (dllPath?: string, dllName?: string) => {
  dllPath = dllPath ? dllPath : DLL_PATH;
  dllName = dllName ? dllName : 'wafer.dll';
  if (process.env.PATH) {
    const pathArray = process.env.PATH.split(';');
    pathArray.unshift(dllPath);
    process.env.PATH = pathArray.join(';');
  }

  const Library = new FFI.Library(path.join(dllPath, dllName), {
    initDataTransmission: ['int', ['size_t']],
    writeDataTransmissionSw: ['int', ['string', 'size_t', 'string', 'size_t']],
    readDataTransmissionSw: [
      'int',
      [
        'string',
        'size_t',
        ref.refType('size_t'),
        ref.refType('uint32'),
        ref.refType('size_t'),
      ],
    ],
    clearAllDataTransmission: ['int', []],
  });

  return Library;
};
export default wafer;
